package sample2.provider.dml;

import java.util.ArrayList;
import java.util.List;

public class SQLWhereBuilder {
	private StringBuilder mWherePart = new StringBuilder();
	private List<String> mSelectionArgs = new ArrayList<String>();

	public String[] getSelectionArgs() {
		return mSelectionArgs.toArray(new String[] {});
	}

	public String getWhere() {
		return '(' + mWherePart.toString() + ')';
	}

	public SQLWhereBuilder addAndEquals(String columns, String value) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}
		mWherePart.append(columns).append("=?");
		mSelectionArgs.add(value);
		return this;
	}
	public SQLWhereBuilder addOrEquals(String columns, String value) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}
		mWherePart.append(columns).append("=?");
		return this;
	}
	public SQLWhereBuilder addAndIn(String columns, String... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}

		StringBuilder sb = new StringBuilder();
		for (String value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(value);
		}

		mWherePart.append(columns).append(" IN (").append(sb).append(')');
		return this;
	}
	public SQLWhereBuilder addOrIn(String columns, String... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}

		StringBuilder sb = new StringBuilder();
		for (String value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(value);
		}

		mWherePart.append(columns).append(" IN (").append(sb).append(')');
		return this;

	}
	public SQLWhereBuilder addAndNotIn(String columns, String... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}

		StringBuilder sb = new StringBuilder();
		for (String value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(value);
		}

		mWherePart.append(columns).append(" NOT IN (").append(sb).append(')');
		return this;

	}
	public SQLWhereBuilder addOrNotIn(String columns, String... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}

		StringBuilder sb = new StringBuilder();
		for (String value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(value);
		}

		mWherePart.append(columns).append(" NOT IN (").append(sb).append(')');
		return this;
	}

	public SQLWhereBuilder addAndEquals(String columns, long value) {
		return addAndEquals(columns, String.valueOf(value));
	}
	public SQLWhereBuilder addOrEquals(String columns, long value) {
		return addOrEquals(columns, String.valueOf(value));
	}
	public SQLWhereBuilder addAndLessThan(String columns, long value) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}
		mWherePart.append(columns).append("<?");
		mSelectionArgs.add(String.valueOf(value));
		return this;
	}
	public SQLWhereBuilder addAndGreaterThan(String columns, long value) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}
		mWherePart.append(columns).append(">?");
		mSelectionArgs.add(String.valueOf(value));
		return this;
	}
	public SQLWhereBuilder addOrLessThan(String columns, long value) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}
		mWherePart.append(columns).append("<?");
		mSelectionArgs.add(String.valueOf(value));
		return this;
	}
	public SQLWhereBuilder addOrGreaterThan(String columns, long value) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}
		mWherePart.append(columns).append(">?");
		mSelectionArgs.add(String.valueOf(value));
		return this;
	}
	public SQLWhereBuilder addAndIn(String columns, long... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}

		StringBuilder sb = new StringBuilder();
		for (long value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(String.valueOf(value));
		}

		mWherePart.append(columns).append(" IN (").append(sb).append(')');
		return this;

	}
	public SQLWhereBuilder addOrIn(String columns, long... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}

		StringBuilder sb = new StringBuilder();
		for (long value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(String.valueOf(value));
		}

		mWherePart.append(columns).append(" IN (").append(sb).append(')');
		return this;

	}
	public SQLWhereBuilder addAndNotIn(String columns, long... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" AND ");
		}

		StringBuilder sb = new StringBuilder();
		for (long value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(String.valueOf(value));
		}

		mWherePart.append(columns).append(" NOT IN (").append(sb).append(')');
		return this;

	}
	public SQLWhereBuilder addOrNotIn(String columns, long... values) {
		if (0 < mWherePart.length()) {
			mWherePart.append(" OR ");
		}

		StringBuilder sb = new StringBuilder();
		for (long value : values) {
			if (0 < sb.length()) {
				sb.append(',');
			}
			sb.append('?');
			mSelectionArgs.add(String.valueOf(value));
		}

		mWherePart.append(columns).append(" NOT IN (").append(sb).append(')');
		return this;
	}
	/*
	 * 
	 */

	public SQLWhereBuilder addAnd(SQLWhereBuilder where) {
		mWherePart.append("  AND (").append(where.toString()).append(')');
		mSelectionArgs.addAll(where.mSelectionArgs);
		return this;
	}

	public SQLWhereBuilder addOr(SQLWhereBuilder where) {
		mWherePart.append("  OR (").append(where.toString()).append(')');
		mSelectionArgs.addAll(where.mSelectionArgs);
		return this;
	}

	@Override
	public String toString() {
		return getWhere();
	}

}
